import {
  api
} from '../../libs/api'


//获取端口属性
export function getCCInfo(ctx) {
  api.onRequest('$method.$get.ccInf.ports', null, null, null, function (msg, reply) {    
    var res = JSON.parse(msg)
    if (res && res.code == 0 && res.data) {     
      ctx.$store.dispatch("kvm/hideComSetting", res.data.comCnt == 0 || res.data.comCnt == 'undefined');
      ctx.$store.dispatch("kvm/hideIrOutSetting", res.data.irOutCnt == 0 || res.data.irOutCnt == 'undefined');
      // ctx.$store.dispatch("media/getInputDevInfo", '');
      ctx.$store.dispatch("kvm/getCCInfo", res.data);
      for (var i = 0; i < res.data.comCnt; i++) {
        ((i)=>{
          getComInfoByIndex(i, ctx);
        })(i)
      }
      // for (var i = 0; i < res.data.irInCnt; i++) {
      //   ((i)=>{
      //     getComInfoByIndex(i);
      //   })(i)
      // }
      for (var i = 0; i < res.data.irOutCnt; i++) {
        ((i)=>{
          getIROutInfoByIndex(i, ctx);
        })(i)
      }
      // for (var i = 0; i < res.data.ioCnt; i++) {
      //   ((i)=>{
      //     getComInfoByIndex(i);
      //   })(i)
      // }
      // for (var i = 0; i < res.data.relayCnt; i++) {
      //   ((i)=>{
      //     getComInfoByIndex(i);
      //   })(i)
      // }
      
    } else {
      ctx.$store.dispatch("media/getInputDevCnt", 0);
    }
  }.bind(ctx)).then(res=>{}, err => {
    ctx.$store.dispatch("kvm/hideComSetting", true);
    ctx.$store.dispatch("kvm/hideIrOutSetting", true);
  })
}

function getComInfoByIndex(i, ctx){
  api.onRequest('$method.$get.ccInf.port.com' + i + '.prop', null, null, null, function (message, reqReply) {
    var data = JSON.parse(message);
    if (data && data.code == 0 && data.data) {
      var req = {
        index: i,
        comPro: data.data
      }
      ctx.$store.dispatch("kvm/getComInfo", req);
    }
  })

  api.onRequest('$method.$get.ccInf.port.com' + i + '.cfg', null, null, null, function (message, reqReply) {
    var data = JSON.parse(message);
    if (data && data.code == 0 && data.data) {
      var req = {
        index: i,
        comCfg: data.data
      }
      ctx.$store.dispatch("kvm/getComInfo", req);
    }
  })
}

function getIROutInfoByIndex(i, ctx){
  api.onRequest('$method.$get.ccInf.port.irOut' + i + '.prop', null, null, null, function (message, reqReply) {    
    var data = JSON.parse(message);
    if (data && data.code == 0 && data.data) {      
      var req = {
        index: i,
        irOutPro: data.data
      }
      ctx.$store.dispatch("kvm/getIrOutInfo", req);
    }
  })

}
